/*
 * OES.java
 *
 * Created on 12-12-2011, 10:14:26
 */
package oes;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.*;

// Main class and frame.
public class OES extends javax.swing.JFrame {

    // Globals
    Calendar cal;
    private int currentID;
    private ArrayList<OESClass> classList;
    private ArrayList<Classroom> roomList;
    private int currentWeek;
    private int currentYear;
    private int currentDay;
    private int typeSelected;
    private JPanel[][] schema;
    private DBConnector dbc;
    private String settingsFileName;
    private ArrayList<OESClass> classes;
    private JFrame frame;

    // Creates new form OES.
    public OES() {
        initComponents();
        setSize(1008, 640);
        setLocation(8, 8);

        // Setup system classes.
        dbc = new DBConnector(this);

        // Build any GUI elements we need.
        buildSchema();

        // Load and set connection info if available.
        settingsFileName = "settings";
        loadSettings();

        // Create a Calendar to manage dates.
        cal = Calendar.getInstance();
        currentDay = cal.get(Calendar.DATE) - (cal.get(Calendar.DAY_OF_WEEK) - 2);
        currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
        currentYear = cal.get(Calendar.YEAR);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - (cal.get(Calendar.DAY_OF_WEEK) - 2));
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DAY_OF_YEAR, cal1.get(Calendar.DAY_OF_YEAR) + 6);

        currentWeekField.setText(cal1.get(Calendar.DATE) + "." + (cal1.get(Calendar.MONTH) + 1) + "." + cal1.get(Calendar.YEAR) + " - " + cal2.get(Calendar.DATE) + "." + (cal2.get(Calendar.MONTH) + 1) + "." + cal2.get(Calendar.YEAR) + ", Week " + currentWeek);


        // Initialize globals.
        typeSelected = 0;               // Are we showing Room, Student, or Teacher lessons?
        classList = new ArrayList<OESClass>();    // To hold class ID's of students or teachers.
        currentID = 0;                  // ID of the selected Room, Student, or Teacher.
        roomList = null;


        //Fills years into year combobox
        for (int i = cal.get(Calendar.YEAR); i < cal.get(Calendar.YEAR) + 4; i++) {
            yearComboBox.addItem(i);
        }

        //Fills lessons into lessonLengthComboBox
        lessonLengthComboBox.removeAllItems();
        int count = 1;
        for (int index = StartTimeComboBox.getSelectedIndex(); index < StartTimeComboBox.getItemCount(); index++) {
            lessonLengthComboBox.addItem(count);
            count++;
        }

    }

    // Update the schema with new lesson info.
    private void updateSchema() {
        CalendarPanel cp = null;
        Lesson l = null;
        Calendar c = Calendar.getInstance();
        int hour = 16;
        for (int y = 1; y < 8; y++) {
            c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
            c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR)-(c.get(Calendar.DAY_OF_WEEK)-2));
            for (int x = 1; x < 8; x++) {
                int cYear = c.get(Calendar.YEAR);
                int cMonth = c.get(Calendar.MONTH) + 1;
                int cDay = c.get(Calendar.DATE);
                cp = (CalendarPanel) schema[x][y];
                cp.setContent(null);
                String date = "" + cYear + "-" + cMonth + "-" + cDay + " " + hour + ":00";
                switch (typeSelected) {
                    case 0:
                        // Rooms
                        l = dbc.getRoomLesson(currentID, date);
                        if (l != null) {
                            cp.setContent(l);
                            cp.setClassName(dbc.getLessonClassName(l.getOclass()));
                            cp.setStaffName(dbc.getLessonStaffName(l.getOclass()));
                        }
                        break;
                    case 1:
                        // Students
                        ArrayList<Lesson> al = null;
                        for (int i = 0; i < classList.size(); i++) {
                            al = dbc.getClassLesson(classList.get(i).getClassID(), date);
                            for (int j = 0; j < al.size(); j++) {
                                l = al.get(j);
                                if (l != null) {
                                    cp.setContent(l);
                                    cp.setClassName(dbc.getLessonClassName(classList.get(i).getClassID()));
                                    cp.setStaffName(dbc.getLessonStaffName(classList.get(i).getClassID()));
                                }
                            }
                        }
                        break;
                    case 2:
                        // Teachers
                        ArrayList<Lesson> alt = null;
                        for (int i = 0; i < classList.size(); i++) {
                            alt = dbc.getClassLesson(classList.get(i).getClassID(), date);
                            for (int j = 0; j < alt.size(); j++) {
                                l = alt.get(j);
                                if (l != null) {
                                    cp.setContent(l);
                                    cp.setClassName(dbc.getLessonClassName(classList.get(i).getClassID()));
                                    cp.setStaffName(dbc.getLessonStaffName(classList.get(i).getClassID()));
                                }
                            }
                        }

                        break;
                }
                c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR)+1);
            }
            hour += 1;
        }
        jMainCalPanel.repaint();
    }

    // Build the panel layout for the Calendar tab.
    private void buildSchema() {
        JLabel l = null;
        schema = new JPanel[8][8];
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                if (x == 0 && y == 0) {
                    schema[x][y] = new JPanel();
                    schema[x][y].setSize(120, 30);
                    schema[x][y].setLocation(0, 0);
                    schema[x][y].setBackground(new Color(159, 174, 191));
                }
                if (x > 0 && y == 0) {
                    schema[x][y] = new JPanel();
                    schema[x][y].setSize(120, 30);
                    schema[x][y].setLocation(x * 122 + 5, 0);
                    schema[x][y].setBackground(new Color(159, 174, 191));
                    switch (x) {
                        case 1:
                            l = new JLabel("Monday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 2:
                            l = new JLabel("Tuesday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 3:
                            l = new JLabel("Wednesday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 4:
                            l = new JLabel("Thursday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 5:
                            l = new JLabel("Friday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 6:
                            l = new JLabel("Saturday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 7:
                            l = new JLabel("Sunday", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                    }
                    schema[x][y].add(l);
                }
                if (x == 0 && y > 0) {
                    schema[x][y] = new JPanel();
                    schema[x][y].setSize(120, 60);
                    schema[x][y].setLocation(0, y * 62 - 25);
                    schema[x][y].setBackground(new Color(159, 174, 191));
                    switch (y) {
                        case 1:
                            l = new JLabel("16:00 - 17:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 2:
                            l = new JLabel("17:00 - 18:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 3:
                            l = new JLabel("18:00 - 19:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 4:
                            l = new JLabel("19:00 - 20:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 5:
                            l = new JLabel("20:00 - 21:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 6:
                            l = new JLabel("21:00 - 22:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                        case 7:
                            l = new JLabel("22:00 - 23:00", JLabel.CENTER);
                            l.setFont(new Font("Serif", Font.BOLD, 14));
                            break;
                    }
                    schema[x][y].add(l);
                }
                if (x > 0 && y > 0) {
                    schema[x][y] = new CalendarPanel();
                    schema[x][y].setSize(120, 60);
                    schema[x][y].setLocation(x * 122 + 5, y * 62 - 25);
                    schema[x][y].setBackground(new Color(239, 241, 230));
                }
                jMainCalPanel.add(schema[x][y]);
                schema[x][y].setBorder(BorderFactory.createLineBorder(Color.black));
            }
        }
        this.validate();
    }

    // Load database connection info and set the TextFields on the Settings page.
    private void loadSettings() {
        if (dbc.loadSettings(settingsFileName)) {
            txtIPField.setText(dbc.getAddress());
            txtPortField.setText(dbc.getPort());
            txtDBField.setText(dbc.getDbname());
            txtUserField.setText(dbc.getUname());
            txtPWField.setText(dbc.getPass());
            dbc.connect();
            //Gets all classes from DB to the Lesson Panel.
            getClasses();
        } else {
            disableTabs();
        }
    }

    private void getClasses() {
        classes = dbc.getClasses();
        for (int i = 0; i < classes.size(); i++) {
            classComboBox.addItem(classes.get(i));
        }
    }

    private boolean validateTime(int year, int month, int day) {
        boolean result = false;
        Calendar current = Calendar.getInstance();
        Calendar check = Calendar.getInstance();
        check.set(year, month - 1, day);
        if (check.after(current)) {
            result = true;
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMainPane = new javax.swing.JTabbedPane();
        jCalendarPane = new javax.swing.JPanel();
        jSearchField = new javax.swing.JTextField();
        jCalendarCombo = new javax.swing.JComboBox();
        jFetchButton = new javax.swing.JButton();
        jMainCalPanel = new javax.swing.JPanel();
        jNextWeekButton = new javax.swing.JButton();
        jPreviousWeekButton = new javax.swing.JButton();
        currentWeekField = new javax.swing.JTextField();
        jLessonPane = new javax.swing.JPanel();
        yearComboBox = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jSearchRoom = new javax.swing.JButton();
        monthComboBox = new javax.swing.JComboBox();
        dayComboBox = new javax.swing.JComboBox();
        roomComboBox = new javax.swing.JComboBox();
        jCreateLesson = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        StartTimeComboBox = new javax.swing.JComboBox();
        lessonLengthComboBox = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        classComboBox = new javax.swing.JComboBox();
        jOptionsPane = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIPField = new javax.swing.JTextField();
        txtPortField = new javax.swing.JTextField();
        txtDBField = new javax.swing.JTextField();
        txtUserField = new javax.swing.JTextField();
        btnSaveSettings = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtPWField = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OES");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jMainPane.setMaximumSize(new java.awt.Dimension(1000, 600));
        jMainPane.setMinimumSize(new java.awt.Dimension(1000, 600));
        jMainPane.setPreferredSize(new java.awt.Dimension(1000, 600));

        jCalendarCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Room", "Student", "Teacher" }));
        jCalendarCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCalendarComboActionPerformed(evt);
            }
        });

        jFetchButton.setText("Fetch");
        jFetchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFetchButtonActionPerformed(evt);
            }
        });

        jMainCalPanel.setLayout(null);

        jNextWeekButton.setText("Next Week");
        jNextWeekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNextWeekButtonActionPerformed(evt);
            }
        });

        jPreviousWeekButton.setText("Previous Week");
        jPreviousWeekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPreviousWeekButtonActionPerformed(evt);
            }
        });

        currentWeekField.setEditable(false);
        currentWeekField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        currentWeekField.setText("Week, Year");

        javax.swing.GroupLayout jCalendarPaneLayout = new javax.swing.GroupLayout(jCalendarPane);
        jCalendarPane.setLayout(jCalendarPaneLayout);
        jCalendarPaneLayout.setHorizontalGroup(
            jCalendarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jCalendarPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jCalendarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jMainCalPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 985, Short.MAX_VALUE)
                    .addGroup(jCalendarPaneLayout.createSequentialGroup()
                        .addComponent(jCalendarCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSearchField, javax.swing.GroupLayout.DEFAULT_SIZE, 850, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jFetchButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jCalendarPaneLayout.createSequentialGroup()
                        .addComponent(jPreviousWeekButton)
                        .addGap(18, 18, 18)
                        .addComponent(currentWeekField, javax.swing.GroupLayout.DEFAULT_SIZE, 761, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jNextWeekButton)))
                .addContainerGap())
        );
        jCalendarPaneLayout.setVerticalGroup(
            jCalendarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jCalendarPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jCalendarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCalendarCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFetchButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jCalendarPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPreviousWeekButton)
                    .addComponent(currentWeekField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jNextWeekButton))
                .addGap(18, 18, 18)
                .addComponent(jMainCalPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
                .addContainerGap())
        );

        jMainPane.addTab("Calendar", jCalendarPane);

        yearComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year" }));
        yearComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yearComboBoxActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel7.setText("Create new lesson:");

        jLabel8.setText("Class");

        jLabel9.setText("Date");

        jSearchRoom.setText("Search for available room");
        jSearchRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jSearchRoomActionPerformed(evt);
            }
        });

        monthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        monthComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                monthComboBoxActionPerformed(evt);
            }
        });

        dayComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day" }));
        dayComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dayComboBoxActionPerformed(evt);
            }
        });

        roomComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Room" }));

        jCreateLesson.setText("Create lesson");
        jCreateLesson.setToolTipText("");
        jCreateLesson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCreateLessonActionPerformed(evt);
            }
        });

        jLabel10.setText("Start time");

        StartTimeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00" }));
        StartTimeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartTimeComboBoxActionPerformed(evt);
            }
        });

        jLabel11.setText("Lesson length");

        classComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Classes" }));

        javax.swing.GroupLayout jLessonPaneLayout = new javax.swing.GroupLayout(jLessonPane);
        jLessonPane.setLayout(jLessonPaneLayout);
        jLessonPaneLayout.setHorizontalGroup(
            jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jLessonPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jLessonPaneLayout.createSequentialGroup()
                        .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9)
                            .addComponent(jSearchRoom)
                            .addComponent(roomComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jLessonPaneLayout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11))
                            .addGroup(jLessonPaneLayout.createSequentialGroup()
                                .addComponent(StartTimeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lessonLengthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jLessonPaneLayout.createSequentialGroup()
                                .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(classComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jLessonPaneLayout.createSequentialGroup()
                                        .addComponent(yearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(monthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(815, Short.MAX_VALUE))
                    .addGroup(jLessonPaneLayout.createSequentialGroup()
                        .addComponent(jCreateLesson, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(896, 896, 896))))
        );
        jLessonPaneLayout.setVerticalGroup(
            jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jLessonPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(classComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yearComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(monthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dayComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jLessonPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StartTimeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lessonLengthComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSearchRoom)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(roomComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCreateLesson)
                .addContainerGap(483, Short.MAX_VALUE))
        );

        jMainPane.addTab("Lessons", jLessonPane);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Database Settings:");

        btnSaveSettings.setText("Save");
        btnSaveSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveSettingsActionPerformed(evt);
            }
        });

        jLabel2.setText("IP / Hostname");

        jLabel3.setText("Port");

        jLabel4.setText("Database name");

        jLabel5.setText("Username");

        jLabel6.setText("Password");

        javax.swing.GroupLayout jOptionsPaneLayout = new javax.swing.GroupLayout(jOptionsPane);
        jOptionsPane.setLayout(jOptionsPaneLayout);
        jOptionsPaneLayout.setHorizontalGroup(
            jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jOptionsPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jOptionsPaneLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jOptionsPaneLayout.createSequentialGroup()
                        .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPortField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addComponent(txtIPField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addComponent(txtDBField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addComponent(txtUserField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                            .addComponent(btnSaveSettings)
                            .addComponent(txtPWField, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))))
                .addGap(813, 813, 813))
        );
        jOptionsPaneLayout.setVerticalGroup(
            jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jOptionsPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtIPField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDBField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jOptionsPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPWField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSaveSettings)
                .addContainerGap(582, Short.MAX_VALUE))
        );

        jMainPane.addTab("Options", jOptionsPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jMainPane, javax.swing.GroupLayout.DEFAULT_SIZE, 1010, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jMainPane, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // SAVE Button.
    private void btnSaveSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveSettingsActionPerformed
        dbc.setAddress(txtIPField.getText());
        dbc.setPort(txtPortField.getText());
        dbc.setDBName(txtDBField.getText());
        dbc.setUName(txtUserField.getText());
        dbc.setPass(String.valueOf(txtPWField.getPassword()));
        if (dbc.saveSettings(settingsFileName)) {
            jMainPane.setEnabledAt(0, true);
            jMainPane.setEnabledAt(1, true);
            //Gets all classes from DB to the Lesson Panel.
            getClasses();
        } else {
            disableTabs();
        }
    }//GEN-LAST:event_btnSaveSettingsActionPerformed

private void StartTimeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartTimeComboBoxActionPerformed

    lessonLengthComboBox.removeAllItems();
    int count = 1;
    for (int index = StartTimeComboBox.getSelectedIndex(); index < StartTimeComboBox.getItemCount(); index++) {
        lessonLengthComboBox.addItem(count);
        count++;
    }
}//GEN-LAST:event_StartTimeComboBoxActionPerformed

    private void jFetchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFetchButtonActionPerformed
        typeSelected = jCalendarCombo.getSelectedIndex();
        if(!(jSearchField.getText().equals(""))) {
            currentID = Integer.decode(jSearchField.getText());
        }
        if(currentID > 0) {
            switch (typeSelected) {
                case 0:
                    // Room
                    break;
                case 1:
                    // Student
                    classList = dbc.getStudentClasses(currentID);
                    break;
                case 2:
                    // Teacher
                    classList = dbc.getTeacherClasses(currentID);
                    break;
            }
            updateSchema();
        }
    }//GEN-LAST:event_jFetchButtonActionPerformed

    private void jCalendarComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCalendarComboActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCalendarComboActionPerformed

private void yearComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yearComboBoxActionPerformed
    dayComboBox.setSelectedIndex(0);
    monthComboBox.setSelectedIndex(0);

    // reset the roomComboBox
    roomComboBox.removeAllItems();
    roomComboBox.addItem("Room");
}//GEN-LAST:event_yearComboBoxActionPerformed

private void monthComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_monthComboBoxActionPerformed
    // make the list of days in a given month
    dayComboBox.removeAllItems();
    dayComboBox.addItem("Day");
    dayComboBox.setSelectedIndex(0);
    try {
        Calendar c;
        int year = Integer.decode("" + yearComboBox.getSelectedItem());
        int month = Integer.decode("" + monthComboBox.getSelectedIndex());
        c = new GregorianCalendar(year, month, 0);
        int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 1; i <= days; i++) {
            dayComboBox.addItem(i);
        }
    } catch (Exception e) {
        // Crap!
    }
    // reset the roomComboBox
    roomComboBox.removeAllItems();
    roomComboBox.addItem("Room");
}//GEN-LAST:event_monthComboBoxActionPerformed

private void dayComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dayComboBoxActionPerformed
    // reset the roomComboBox
    roomComboBox.removeAllItems();
    roomComboBox.addItem("Room");
}//GEN-LAST:event_dayComboBoxActionPerformed

    private void jCreateLessonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCreateLessonActionPerformed
        //TO DO: Insert lesson into db (Check if it's valid first!)
        if (roomComboBox.getSelectedIndex() > 0) {
            String date = "" + yearComboBox.getSelectedItem() + "-" + monthComboBox.getSelectedIndex() + "-" + dayComboBox.getSelectedItem();
            String time = "" + StartTimeComboBox.getSelectedItem();
            Classroom cr = (Classroom) roomComboBox.getSelectedItem();
            int ri = cr.getRoomid();
            OESClass oc = (OESClass) classComboBox.getSelectedItem();
            int ci = oc.getClassID();
            dbc.insertLesson(date + " " + time, ci, ri);
            JOptionPane.showMessageDialog(this, "Lesson created", "Info", 1);
        } else {
            JOptionPane.showMessageDialog(this, "No room selected!", "Error!", 0);
        }
    }//GEN-LAST:event_jCreateLessonActionPerformed

    private boolean checkLessonPane() {
        boolean result = true;
        if (classComboBox.getSelectedItem() == "Class") {
            result = false;
        }
        if (yearComboBox.getSelectedItem() == "Year") {
            result = false;
        }
        if (monthComboBox.getSelectedIndex() == 0) {
            result = false;
        }
        if (dayComboBox.getSelectedItem() == "Day") {
            result = false;
        }
        return result;
    }

    private void jSearchRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchRoomActionPerformed
        // TO DO: Need to check for valid dates and stuff...!
        try {
            String toReplace = (String) StartTimeComboBox.getSelectedItem();
            int year = (Integer) yearComboBox.getSelectedItem();
            int month = monthComboBox.getSelectedIndex();
            int day = (Integer) dayComboBox.getSelectedItem();
            int clean = Integer.decode(toReplace.replace(":00", ""));
            int lessonLength = (Integer) lessonLengthComboBox.getSelectedItem();
            if (validateTime(year, month, day)) {
                try {
                    roomList = dbc.getAvailableRooms(year, month, day, clean, lessonLength);
                    if (roomList.size() > 0) {
                        roomComboBox.removeAllItems();
                        roomComboBox.addItem("Room");
                        for (int i = 0; i < roomList.size(); i++) {
                            roomComboBox.addItem(roomList.get(i));
                        }
                    } else {
                        roomComboBox.removeAllItems();
                        roomComboBox.addItem("Room");
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(frame, "Error in selection: " + e.getMessage(), "Error!", 0);
                }
            } else {
                JOptionPane.showMessageDialog(this, "The date is invalid", "Info", 0);
                roomComboBox.removeAllItems();
                roomComboBox.addItem("Room");
            }
        } catch (Exception e) {
            System.out.println("Crap!");
        }
    }//GEN-LAST:event_jSearchRoomActionPerformed

    private void jPreviousWeekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPreviousWeekButtonActionPerformed
        // TODO add your handling code here:
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 7);
        currentDay = cal.get(Calendar.DATE) - (cal.get(Calendar.DAY_OF_WEEK) - 2);
        currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
        currentYear = cal.get(Calendar.YEAR);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - (cal.get(Calendar.DAY_OF_WEEK) - 2));
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DAY_OF_YEAR, cal1.get(Calendar.DAY_OF_YEAR) + 6);

        currentWeekField.setText(cal1.get(Calendar.DATE) + "." + (cal1.get(Calendar.MONTH) + 1) + "." + cal1.get(Calendar.YEAR) + " - " + cal2.get(Calendar.DATE) + "." + (cal2.get(Calendar.MONTH) + 1) + "." + cal2.get(Calendar.YEAR) + ", Week " + currentWeek);
        updateSchema();
    }//GEN-LAST:event_jPreviousWeekButtonActionPerformed

    private void jNextWeekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNextWeekButtonActionPerformed
        // TODO add your handling code here:
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 7);
        currentDay = cal.get(Calendar.DATE) - (cal.get(Calendar.DAY_OF_WEEK) - 2);
        currentWeek = cal.get(Calendar.WEEK_OF_YEAR);
        currentYear = cal.get(Calendar.YEAR);
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - (cal.get(Calendar.DAY_OF_WEEK) - 2));
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DAY_OF_YEAR, cal1.get(Calendar.DAY_OF_YEAR) + 6);

        currentWeekField.setText(cal1.get(Calendar.DATE) + "." + (cal1.get(Calendar.MONTH) + 1) + "." + cal1.get(Calendar.YEAR) + " - " + cal2.get(Calendar.DATE) + "." + (cal2.get(Calendar.MONTH) + 1) + "." + cal2.get(Calendar.YEAR) + ", Week " + currentWeek);
        updateSchema();
    }//GEN-LAST:event_jNextWeekButtonActionPerformed

    private void disableTabs() {
        jMainPane.setSelectedIndex(2);
        jMainPane.setEnabledAt(0, false);
        jMainPane.setEnabledAt(1, false);
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;








                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OES.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OES().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox StartTimeComboBox;
    private javax.swing.JButton btnSaveSettings;
    private javax.swing.JComboBox classComboBox;
    private javax.swing.JTextField currentWeekField;
    private javax.swing.JComboBox dayComboBox;
    private javax.swing.JComboBox jCalendarCombo;
    private javax.swing.JPanel jCalendarPane;
    private javax.swing.JButton jCreateLesson;
    private javax.swing.JButton jFetchButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jLessonPane;
    private javax.swing.JPanel jMainCalPanel;
    private javax.swing.JTabbedPane jMainPane;
    private javax.swing.JButton jNextWeekButton;
    private javax.swing.JPanel jOptionsPane;
    private javax.swing.JButton jPreviousWeekButton;
    private javax.swing.JTextField jSearchField;
    private javax.swing.JButton jSearchRoom;
    private javax.swing.JComboBox lessonLengthComboBox;
    private javax.swing.JComboBox monthComboBox;
    private javax.swing.JComboBox roomComboBox;
    private javax.swing.JTextField txtDBField;
    private javax.swing.JTextField txtIPField;
    private javax.swing.JPasswordField txtPWField;
    private javax.swing.JTextField txtPortField;
    private javax.swing.JTextField txtUserField;
    private javax.swing.JComboBox yearComboBox;
    // End of variables declaration//GEN-END:variables
}
